////--------------------------------------------------------------------------------------
//// File: Tutorial02.fx
////
//// Copyright (c) Microsoft Corporation. All rights reserved.
////--------------------------------------------------------------------------------------

//struct VsInput
//{	
//	float4 pos : POSITION;
//	float4 colour : COLOR;
//};


//struct VsOutput
//{
//	float4 pos : SV_POSITION;
//	float4 colour : COLOR;
//};

////--------------------------------------------------------------------------------------
//// Vertex Shader
////--------------------------------------------------------------------------------------
//VsOutput VS(VsInput input)
//{
//	VsOutput output = (VsOutput)0;
//	output.pos = input.pos;
//	output.colour = input.colour;

//	output.pos += float4(0, 0, 0, 0.8f);

//	return output;
//}

////--------------------------------------------------------------------------------------
//// Pixel Shader
////--------------------------------------------------------------------------------------
//float4 PS(VsOutput output) : SV_Target
//{
//	return output.colour;

//	//return float4(1, 0, 0, 1);

//    //return float4( 0.5f, 1.0f, 0.0f, 1.0f );    // Yellow, with Alpha = 1
//}


//--------------------------------------------------------------------------------------
// File: Tutorial04.fx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

struct Light
{
	float4 direction;
	float4 colour;
	float4 ambient;
	float4 position;
	float4 attenuation;
	float2 pad1;
	float range;
	int enabled;
};

//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
cbuffer ConstantBuffer : register( b0 )
{
	float4 TEST;
	matrix World;
	matrix View;
	matrix Projection;
	matrix LightViewMatrix;
	matrix LightProjMatrix;
	Light light[3];
	float4 OutputColour;
	float2 m_texturePositionOne;
	float2 TexturePositionTwo;
	float3 CameraPosition;
}
//--------------------------------------------------------------------------------------
struct VS_INPUT // = PS_INPUT
{
    float4 Pos : POSITION;
    float2 TexCoord : TEXCOORD0;
	float3 Normal : NORMAL0;
	float3 Tangent : TANGENT;
	float3 Binormal : BINORMAL;
};

struct PS_INPUT
{
	float4 Pos : SV_POSITION;
	float2 TexCoord : TEXCOORD0;
	float3 Normal : NORMAL0;
	float3 Tangent : TANGENT;
	float3 Binormal : BINORMAL;
	float4 HeightMapPos : TEXCOORD1;
	float4 LightViewPosition : TEXCOORD2;
	//float4 DepthPos : TEXCOORD3;
};


Texture2D ObjTexture : register( t0 );
Texture2D ObjTexture2 : register( t1 );
Texture2D ObjTexture3 : register ( t2 );
Texture2D ObjTexture4 : register ( t3 );
Texture2D ObjTexture5 : register ( t4 );
SamplerState ObjSamplerState : register( s0 );

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output = (PS_INPUT)0;
	
	//output.Pos = LightDirection[0];


    output.Pos = mul( input.Pos, World );
    output.Pos = mul( output.Pos, View );
    output.Pos = mul( output.Pos, Projection );
	
	output.Normal = mul(input.Normal, World);


	//output.Pos = mul(Pos, World);

    output.TexCoord = input.TexCoord;
	output.HeightMapPos = mul(input.Pos, World);

	//float4 s0 = ObjTexture.Sample(ObjSamplerState, output.TexCoord);
	//output.Pos += s0.r;
	//output.DepthPos = output.Pos;


    return output;
}


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(PS_INPUT input) : SV_Target
{
	float4 diffuse = 0;
	float4 floorTex = ObjTexture.Sample(ObjSamplerState, input.TexCoord * 16.0f);// * 2.0f);
	float4 terrainMap = ObjTexture2.Sample(ObjSamplerState, input.TexCoord);
	float4 dipTex = ObjTexture3.Sample(ObjSamplerState, input.TexCoord * 32.0f);
	float4 rockTexture = ObjTexture4.Sample(ObjSamplerState, input.TexCoord * 16.0f);
	float4 detailMap = ObjTexture5.Sample(ObjSamplerState, input.TexCoord * 64.0f);

	if (OutputColour.a > 0.0f)
	{
		float4 blendedTexture = (lerp(dipTex, floorTex, 0.8f));
		diffuse = ((terrainMap * 1.5f) * blendedTexture) + ((1.0f - terrainMap) * floorTex);// * floorTex);
		diffuse *= OutputColour;
	}
	else
	{
		diffuse = floorTex;
		diffuse *= OutputColour;
	}

	input.Normal = normalize(input.Normal);

	if (input.Normal.y < 0.9f)
	{
		diffuse = rockTexture * 0.5f;
	}
	else if (input.Normal.y > 0.89f && input.Normal.y < 0.95f)
	{
		diffuse = lerp(rockTexture, floorTex, 0.8f);
	}

// slope texturing end

	float depthVal = input.Pos.z / input.Pos.w;
	if (depthVal < 0.02f)
	{
	}
	else
	{
		diffuse *= detailMap * 2.0f;
	}

	if (light[1].enabled)
	{
		diffuse += saturate(dot((float3)light[1].direction, input.Normal));
	}
	diffuse *= (light[1].colour * 0.5f);
	
	return diffuse;
}

float4 PSSolid( PS_INPUT input) : SV_Target
{
    return float4(0, 0, 0, 0);
}
